package net.libra.scrollui
{
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	
	import net.libra.ui.base.BaseSprite;
	import net.libra.utils.ObjectPool;
	

	/**
	 *
	 *@author elvisjiang
	 *@createTime 2011-7-14
	 */
	public class MyList extends BaseSprite
	{
		//数据
		private var _source:Array;
		//item
		protected var _pool:ObjectPool;
		//垂直间隔
		private var _verticalGap:Number;
		private var _itemWidth:Number;
		private var _itemHeight:Number;//item高度，仅仅用来测量数据来了之后 的高度。
		private var _initHeight:Number;//首次呈现出来的height
		
		public function MyList(itemRenderer:Class,itemWidth:Number,itemHeight:Number,verticalGap:Number)
		{
			_itemWidth = itemWidth;
			_itemHeight = itemHeight;
			_verticalGap = verticalGap;
			_pool = new ObjectPool(itemRenderer);
			_source = [];
			init();
		}
		protected function init():void
		{
			mouseEnabled = true;
			mouseChildren = true;
		}
		public function get source():Array
		{
			return _source;
		}
		public function set source(value:Array):void
		{
			_source = value||[];
			update();
			_initHeight = _source.length * _itemHeight + (_source.length - 1) * verticalGap;
		}
		
		override protected function render():void
		{
			super.render();
			
			removeAll();
			updateVertical();
			
			this.dispatchEvent(new ListEvent(ListEvent.SOURCE_RENDER,false));
		}
		override public function destroy():void
		{
			_source = [];
			
			removeAll();
			while(_pool.length > 0)
			{
				var item:ListItem = _pool.borrowObject() as ListItem;
				if(item)
				{
					item.data = null;
					item.destroy();
				}
				
			}
			_pool.destroy();
			_pool = null;
			super.destroy();
		}
		//移除所有item,并回收对象池
		protected function removeAll():void
		{
			while(numChildren > 0)
			{
				var item:ListItem = removeChildAt(0) as ListItem;
				if(item)
				{
					_pool.returnObject(item);
				}
			}
		}
		private var _itemArray:Array;

		public function get itemArray():Array
		{
			return _itemArray;
		}

		public function set itemArray(value:Array):void
		{
			_itemArray = value;
		}

		protected function updateVertical():void
		{
			_itemArray = [];
			
			for(var i:uint = 0; i < _source.length ;i++)
			{
				var item:ListItem = _pool.borrowObject() as ListItem;
				if(!item)
				{
					throw new Error("ListItem必须实现ListItem");
				}
				
				item.data = _source[i];
				
				////////////////////////////////////////////////////////
				_itemArray[i] = item;
				
				addChild(item);
			}
//			VLayout.layout(this,verticalGap);
			layout();
		}
		private function layout():void
		{
			var items:Array = getItems();
			var top:Number = 0;
			for(var i:int = 0; i < items.length; i++)
			{
				items[i].y = top;
				top += items[i].height + verticalGap;
			}
		}
		/**
		 * 得到总内容高度
		 */ 
		protected function get contentHeight():Number
		{
			var items:Array = getItems();
			var totalHeight:Number = 0;
			for(var i:int = 0; i < items.length; i++)
			{
				totalHeight += items[i].height + verticalGap;
			}
			return totalHeight >> 0;//取整
		}
		private function getItems():Array
		{
			var result:Array = new Array();
			for(var i:int = 0; i < numChildren; i++)
			{
				var item:ListItem = getChildAt(i) as ListItem;
				if(item)
					result.push(item);
			}
			return result;
		}
		
		///////////////////////////////////////////////////////////
		public function get initHeight():Number
		{
			return _initHeight;
		}
		public function get itemHeight():Number
		{
			return _itemHeight;
		}
		public function get itemWidth():Number
		{
			return _itemWidth;
		}
		public function get verticalGap():Number
		{
			return _verticalGap;
		}
		
	}
}